package conson.tech.manage.message.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import conson.tech.manage.api.message.constant.MessageConstant;
import conson.tech.core.constant.SecurityConstants;
import conson.tech.core.response.Response;
import conson.tech.manage.message.entity.pojo.MessageConfig;
import conson.tech.manage.message.entity.vo.MailMsgConfigVO;
import conson.tech.manage.message.entity.vo.MessageConfigVo;
import conson.tech.manage.message.entity.vo.MessageSendLogVo;
import conson.tech.manage.message.entity.vo.PageVo;
import conson.tech.manage.message.service.MessageConfigService;
import conson.tech.manage.message.service.MessageSendLogService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * ClassName: MessageBackstageController
 * Description:
 * Author: HXL
 * Date: 2020/4/22 9:39 上午
 * Version: 1.0
 */
@Api(tags = "消息相关后台接口")
@RestController
@RequestMapping(SecurityConstants.MANAGERAPI_MATCHER +"/msg/")
public class MessageBackstageController {


    @Autowired
    private MessageSendLogService messageSendLogService;
    @Autowired
    private MessageConfigService messageConfigService;

    @PostMapping("config/preview")
    public Response previewMailMsg(HttpServletRequest request, @RequestBody MailMsgConfigVO vo){
        MessageConfig messageConfig = new MessageConfig();
        BeanUtils.copyProperties(vo,messageConfig);
        //发送预览信息
        if (!StringUtils.isEmpty(vo.getPreviewMobile())){

        }
        //messageConfig.setSendType(MessageConstant.SEND_TYPE_ONE);
        //messageConfigService.createMessage(messageConfig);
        return Response.success();
    }
    //提交站内信
    @PostMapping("config/submit")
    public Response submitMailMsg(HttpServletRequest request, @RequestBody MailMsgConfigVO vo){
        MessageConfig messageConfig = new MessageConfig();
        BeanUtils.copyProperties(vo,messageConfig);
        if (vo.getReleaseWay() != null && vo.getReleaseWay() == MessageConstant.RELEASE_WAY_ONE){
            vo.setReleaseStartTime(new Date());
        }
       /* else if (vo.getReleaseWay() != null && vo.getReleaseWay() == MessageConstant.SEND_TYPE_TWO){

        }*/
        messageConfig.setSendType(MessageConstant.SEND_TYPE_ONE);
        messageConfig.setDeviceType(MessageConstant.DEVICE_TYPE_ZERO);
        messageConfigService.createMessage(messageConfig);
        return Response.success();
    }

    //查询消息配置
    @PostMapping("config/queryPage")
    public Response<Page<MessageConfigVo>> queryMessagePage(@RequestBody PageVo vo){
        //if (vo == null || StringUtils.isEmpty(vo.getName()))
        Page<MessageConfigVo> page= messageConfigService.queryMessagePage(vo);
        return Response.success(page);
    }

    //创建消息发送
    @PostMapping("send/create")
    public Response createSendLog(@RequestBody MessageSendLogVo vo){
        //if (vo == null || StringUtils.isEmpty(vo.getName()))
        messageSendLogService.createSendLog(vo);
        return Response.success();
    }

    //查询消息发送
    @PostMapping("send/queryPage")
    public Response<Page<MessageSendLogVo>> querySendPage(@RequestBody PageVo vo){
        //if (vo == null || StringUtils.isEmpty(vo.getName()))
        Page<MessageSendLogVo> page= messageSendLogService.querySendPage(vo);
        return Response.success(page);
    }

}
